package com.fs.system.student.service;

import cn.hutool.core.util.ObjectUtil;
import com.fs.system.common.ResponseData;
import com.fs.system.exception.CustomerException;
import com.fs.system.manager.domain.Account;
import com.fs.system.student.domain.ApplicationRecord;
import com.fs.system.student.domain.Student;
import com.fs.system.student.mapper.StudentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class StudentService {

    @Resource
    private StudentMapper studentMapper;
    public ResponseData getStudentInfo(Student student) {
        List<Student> studentInfo = studentMapper.getStudentInfo(student);
        return new ResponseData("200","查询成功",studentInfo);
    }

    public ResponseData updateStudentInfo(Student student) {
            if (student.getStuId() == null) {
                throw new CustomerException("500","更新不合法");
            }
            Student dbStudent = studentMapper.selectStudentById(student.getStuId());
            if(Objects.isNull(dbStudent)){
                throw new CustomerException("500","更新不合法");
            }
            student.setUpdateTime(LocalDateTime.now());
            studentMapper.updateStudentInfo(student);
            return new ResponseData("200","更新成功");
    }

    /**
     * 学生登录方法
     * @param account
     * @return
     */
    public Map<String, Object> login(Account account) {
        Map<String, Object> resMap = new HashMap<>();
        Student dbStudent = studentMapper.selectByUsername(account.getUsername());
        if(Objects.isNull(dbStudent)){
            throw new CustomerException("500","用户名不存在");
        }
        if(!"0".equals(dbStudent.getStatus())){
            throw new CustomerException("500","用户状态不合法");
        }
        //用户存在
        // 判断密码是否正确
        if(!dbStudent.getPassword().equals(account.getPassword())){
            throw new CustomerException("500","用户名或密码错误");
        }
        //所有逻辑判断没有问题，返回前端所需
        resMap.put("stuId", dbStudent.getStuId());
        resMap.put("username", dbStudent.getUsername());
        resMap.put("realname", dbStudent.getRealname());
        resMap.put("classId", dbStudent.getClassId());
        resMap.put("specialityId", dbStudent.getSpecialityId());
        resMap.put("gradeId", dbStudent.getGrade());
        resMap.put("collegeId", dbStudent.getCollegeId());
        resMap.put("status", dbStudent.getStatus());
        resMap.put("role", dbStudent.getRole());
        return resMap;
    }

    /**
     * 学生修改密码方法
     * @param account
     */
    public void updatePassword(Account account) {
        /*从数据库拿出对象*/
        Student dbStudent = studentMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbStudent)) {
            throw new CustomerException("500","用户不存在");
        }
        /*判断原本输入传过来的原密码和查到的用户在数据库的密码是否一致*/
        if (!account.getPassword().equals(dbStudent.getPassword())) {
            throw new CustomerException("500","原密码错误");
        }
        dbStudent.setPassword(account.getNewPassword());
        dbStudent.setUpdateBy(account.getUsername());
        dbStudent.setUpdateTime(LocalDateTime.now());
        /*将更新过的对象再更新到数据库里面*/
        studentMapper.updateStudentInfo(dbStudent);
    }

    public ResponseData deleteStudent(Long stuId, String updateBy) {
        Student student = studentMapper.selectStudentById(stuId);
        if(Objects.isNull(student)){
            throw new CustomerException("500","删除不合法");
        }
        studentMapper.updateStudentIsDeleted(stuId,updateBy);
        return new ResponseData("200","删除成功");
    }

    public PageInfo<Student> selectPage(Integer pageNum, Integer pageSize, Student student) {
        PageHelper.startPage(pageNum,pageSize);
        List<Student> studentInfo = studentMapper.getStudentInfo(student);
        return PageInfo.of(studentInfo);
    }

    public ResponseData addOneStudent(Student student) {
        Student dbStudent = studentMapper.selectByUsername(student.getUsername());
        if(Objects.nonNull(dbStudent)){
            throw new CustomerException("500","用户名已存在");
        }
        if(Objects.isNull(student)){
            throw new CustomerException("500","新增不合法");
        }
        student.setPassword("123456");
        student.setRole("STUDENT");
        student.setScore(0L);
        student.setDeleted(false);
        student.setStatus("0");
        studentMapper.insert(student);
        return new ResponseData("200","添加成功");
    }

    public ResponseData deleteBatch(List<Long> ids, String updateBy) {
        for(Long stuId:ids){
            System.out.println(stuId);
            this.deleteStudent(stuId,updateBy);
        }
        return new ResponseData("200","批量删除成功");
    }

    public Integer selectUserCountByDateTime(LocalDateTime startOfDay, LocalDateTime endOfDay, Long collegeId) {
        return studentMapper.countStudentByUpdateTime(startOfDay, endOfDay, collegeId);
    }
}
